package com.hy.frame.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
 * title 无
 * author heyan
 * time 19-7-10 下午4:24
 * desc 无
 */
public class AutoFrameLayout extends FrameLayout implements IAutoDesign {

    protected DesignLayoutHelper designHelper;

    public AutoFrameLayout(Context context) {
        this(context, null);
    }

    public AutoFrameLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs, defStyleAttr);
    }

    @SuppressLint("CustomViewStyleable")
    private void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
        if (designHelper == null) {
            designHelper = new DesignLayoutHelper(this);
        }
        designHelper.loadAttributes(attrs, defStyleAttr);
        designHelper.loadBackgroundAttributes(attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        designHelper.onLayout(getWidth(), getHeight());
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        ViewGroup.LayoutParams newParams = params;
        if (params == null)
            newParams = generateDefaultLayoutParams();
        if (designHelper != null)
            designHelper.fitLayoutParams(newParams);
        super.addView(child, index, newParams);
    }

    @Override
    public void onViewAdded(View child) {
        super.onViewAdded(child);
        //是否强制缩放子View
        if (designHelper != null)
            designHelper.onViewAdded(child);
    }

    @Override
    public boolean isScaleEnabled() {
        return designHelper.scaleEnabled;
    }

    @Override
    public DesignHelper getDesignHelper() {
        return designHelper;
    }
}
